package com.mzkj.wanjulaile.mobile.api.modules.order.entity;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * Created by ianly on 16/12/13.
 */
public class DSHOrder implements Delayed {

    private String orderId;
    private long startTime;

    public DSHOrder() {

    }

    /**
     * orderId:订单id
     * timeout：自动收货的超时时间，秒
     */
    public DSHOrder(String orderId, int timeout) {
        this.orderId = orderId;
        this.startTime = System.currentTimeMillis() + timeout * 1000L;
    }


    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(startTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed other) {
        if (other == this) {
            return 0;
        }
        if (other instanceof DSHOrder) {
            DSHOrder otherRequest = (DSHOrder) other;
            long otherStartTime = otherRequest.getStartTime();
            return (int) (this.startTime - otherStartTime);
        }

        return 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DSHOrder dshOrder = (DSHOrder) o;

        if (orderId != dshOrder.orderId) return false;
        return startTime == dshOrder.startTime;

    }

    @Override
    public int hashCode() {
        int result = orderId != null ? orderId.hashCode() : 0;
        result = 31 * result + (int) (startTime ^ (startTime >>> 32));
        return result;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    @Override
    public String toString() {
        return "DSHOrder{" +
                "orderId=" + orderId +
                ", startTime=" + startTime +
                '}';
    }
}
